package com.qiwei.puding.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.qiwei.puding.app.bean.GithubUser;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OkGo.getInstance().init(getApplication());
        setContentView(R.layout.activity_main);
        mTextMessage = findViewById(R.id.message);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        // 网络请求
        // https://github.com/jeasonlzy/okhttp-OkGo/wiki/OkGo
        OkGo.<GithubUser>get("https://api.github.com/users/mrhuangqiwei")
                .execute(new JsonCallback<GithubUser>() {
                    @Override
                    public void onStart(Request<GithubUser, ? extends Request> request) {
                        super.onStart(request);
                        //loading
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        //xiaoshi
                    }

                    @Override
                    public void onSuccess(Response<GithubUser> response) {
                        // 获取到了bean
                        GithubUser myUser = response.body();
                        Log.e("TAG", myUser.login);
                        Toast.makeText(MainActivity.this,
                                myUser.avatar_url, Toast.LENGTH_SHORT).show();
                        mTextMessage.setText(myUser.html_url);
                    }
                });
    }
}
